
package com.femass.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import com.femass.model.Autor;
import com.femass.repository.AutoreRepository;
import com.femass.util.Repositorios;

@FacesConverter("autorConverter")
public class AutorConverter implements Converter{

    private Repositorios repositorios = new Repositorios();
    private AutoreRepository repoAutores = this.repositorios.getAutores();
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Autor autor = null;
        
        if(!value.equals("")){
            return (Autor) this.repoAutores.porCodigo(new Integer(value));
            
        }
        
        return autor;
        
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String retorno = null;
        
        if(value!=null){
            Integer codigo = ((Autor)value).getCod();
            return codigo == null ? "" : codigo.toString();
        }
        
        return retorno;
    }
    
}
